package com.boot.rabbitmq.apidemo.model.topic;

import com.boot.rabbitmq.apidemo.model.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * 主题模式
 */
public class Send {

    private final static String EXCHANGE_NAME = "test_exchange_topic";

    public static void send(String message,String key) throws Exception {
        // 获取到连接以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // 声明exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        // 消息内容
        channel.basicPublish(EXCHANGE_NAME, key, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

        channel.close();
        connection.close();
    }

    public static void main(String[] args) {

        for (int i = 0; i < 100; i++) {
            try {
                send("message_routekey"+i,"routekey.GG");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        for (int i = 0; i < 100; i++) {
            try {
                send("message_test"+i,"test.GG");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
